//package com.plian.system.service.dj.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.config.YthTaskObserver;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.dj.DjPartymemberBaseInfo;
//import com.plian.system.mapper.dj.DjPartymemberBaseinfoMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.R;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.properties.GzwProperties;
//import com.plian.system.service.dj.IDjPartymemberBaseinfoService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.sys.user.ISysUserService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.service.yth.IPartyBaseInformationService;
//import com.plian.system.vo.dj.DjPartymemberBaseInfoVO;
//import com.plian.system.wrapper.dj.DjPartymemberBaseInfoWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
//@Slf4j
//@Service
//@AllArgsConstructor
//public class DjPartymemberBaseinfoServiceImpl extends BaseServiceImpl<DjPartymemberBaseinfoMapper, DjPartymemberBaseInfo> implements IDjPartymemberBaseinfoService, BaseFlowService {
//
//
//    private FormWorkflowService formWorkflowService;
//
//    private IFormCodeService formCodeService;
//
//    private final DjPartymemberBaseInfoWrapper djPartymemberBaseinfoWrapper;
//
//    private ISysUserService userService;
//    private IOrgService orgService;
//
//    private GzwProperties gzwProperties;
//
//    private IPartyBaseInformationService partyBaseInformationService;
//
//
//    @Override
//    @Transactional(rollbackFor = Throwable.class)
//    public boolean submit(DjPartymemberBaseInfo djPartymemberBaseinfo) {
//        if (!Optional.ofNullable(djPartymemberBaseinfo.getStatus()).isPresent()) {
//            djPartymemberBaseinfo.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//        return saveOrUpdate(djPartymemberBaseinfo);
//    }
//
//    @Override
//    public boolean saveOrUpdate(DjPartymemberBaseInfo djPartymemberBaseinfo) {
//        String loginName = userService.getById(TokenUtil.getTokenUserId()).getUsername();
//        boolean flag = false;
//        if (djPartymemberBaseinfo.getId() == null) {
//            flag = true;
//            djPartymemberBaseinfo.setCode(formCodeService.getCode(FormTypeConstant.DJ_PARTYMEMBER_EVALUATION));
//            djPartymemberBaseinfo.setFormType(String.valueOf(FormTypeConstant.DJ_PARTYMEMBER_EVALUATION));
//            djPartymemberBaseinfo.setFillingTime(new Date());
//            djPartymemberBaseinfo.setFillingCreateUserCode(TokenUtil.getTokenUserId());
//            djPartymemberBaseinfo.setFillingCreateUserName(loginName);
//            if (StringUtil.isBlank(djPartymemberBaseinfo.getFillingUnitId())) {
//                djPartymemberBaseinfo.setFillingUnitId(TokenUtil.getTokenOrgId());
//            }
//            if (StringUtil.isBlank(djPartymemberBaseinfo.getFillingUnitName())) {
//                djPartymemberBaseinfo.setFillingUnitName(TokenUtil.getCompetentOrganization());
//            }
//        }
//        boolean result = super.saveOrUpdate(djPartymemberBaseinfo);
//        return result;
//    }
//
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            DjPartymemberBaseInfo djPartymemberBaseinfo = getById(id);
//            if (djPartymemberBaseinfo == null) {
//                return false;
//            } else if (djPartymemberBaseinfo.getStatus() != null &&
//                    (djPartymemberBaseinfo.getStatus() != FormStatusConstant.STASH && djPartymemberBaseinfo.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        boolean result = super.deleteLogic(ids);
//        return result;
//    }
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        /**
//         * 设置审批人和审批时间
//         */
//        DjPartymemberBaseInfo djPartymemberBaseinfo = getById(id);
//        djPartymemberBaseinfo.setApprovedUser(TokenUtil.getTokenUserId());
//        Date now = DateUtil.now();
//        djPartymemberBaseinfo.setApprovedTime(now);
//        updateById(djPartymemberBaseinfo);
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        List<DjPartymemberBaseInfo> djPartymemberBaseinfos = list(new LambdaQueryWrapper<DjPartymemberBaseInfo>()
//                .in(DjPartymemberBaseInfo::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA))));
//        List<DjPartymemberBaseInfoVO> djPartymemberBaseinfoVOList = djPartymemberBaseinfoWrapper.entityToVO(djPartymemberBaseinfos);
//        List<PageData> pageDataList = new ArrayList<>();
//        try {
//            if (CollectionUtil.isNotEmpty(djPartymemberBaseinfoVOList)) {
//                for (DjPartymemberBaseInfoVO vo : djPartymemberBaseinfoVOList) {
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getFillingUnitName());
//                    pd.put("APPLICANTNAME", vo.getFillingCreateUserName());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getFillingUnitId());
//                    pd.put("formType", FormTypeConstant.DJ_PARTYMEMBER_EVALUATION);
//                    pd.put("orgName", vo.getFillingUnitName());
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.DJ_PARTYMEMBER_EVALUATION);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     *
//     * @param id           主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            DjPartymemberBaseInfo djPartymemberBaseinfo = getById(id);
//            djPartymemberBaseinfo.setStatus(updateStatus);
//            updateById(djPartymemberBaseinfo);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//
//    @Override
//    public R analysePartyMember(DjPartymemberBaseInfoVO djPartymemberBaseinfoVO, String flag) {
//
//        String fillingUnitId = djPartymemberBaseinfoVO.getFillingUnitId();
//        if (StringUtil.isBlank(fillingUnitId)) {
//            return R.data("填报单位不能为空", CommonCode.FAIL);
//        }
//        List<PageData> result = new ArrayList<>();
//
//        int gtAge50Num = 0;
//        int age3050Num = 0;
//        int age2535Num = 0;
//        int ltAge25Num = 0;
//        int ltJuniorCollegeNum = 0;
//        int juniorCollegeNum = 0;
//        int undergraduateNum = 0;
//        int masterNum = 0;
//        int doctorateNum = 0;
//        int postdoctorNum = 0;
//
//        String queryUnitId = null;
//
//
//        Integer isGzw = djPartymemberBaseinfoVO.getIsGzw();
//        if (isGzw != 1) {
//            queryUnitId = fillingUnitId;
//        }
//
//        List<DjPartymemberBaseInfo> allMembers = baseMapper.findAll(queryUnitId);
//        if (CollectionUtil.isNotEmpty(allMembers)) {
//
//            for (DjPartymemberBaseInfo item : allMembers) {
//                gtAge50Num = item.getGtAge50Num() + gtAge50Num;
//                age3050Num = item.getAge3050Num() + age3050Num;
//                age2535Num = item.getAge2535Num() + age2535Num;
//                ltAge25Num = item.getLtAge25Num() + ltAge25Num;
//                ltJuniorCollegeNum = item.getLtJuniorCollegeNum() + ltJuniorCollegeNum;
//                juniorCollegeNum = item.getJuniorCollegeNum() + juniorCollegeNum;
//                undergraduateNum = item.getUndergraduateNum() + undergraduateNum;
//                masterNum = item.getMasterNum() + masterNum;
//                doctorateNum = item.getDoctorateNum() + doctorateNum;
//                postdoctorNum = item.getPostdoctorNum() + postdoctorNum;
//            }
//        }
//        PageData pageData = new PageData();
//        switch (flag) {
//            case "age":
//                pageData.put(">50周岁人数", gtAge50Num);
//                pageData.put("30-50周岁人数", age3050Num);
//                pageData.put("25-35周岁人数", age2535Num);
//                pageData.put("<25周岁人数", ltAge25Num);
//                break;
//            case "edu":
//                pageData.put("大专以下人数", ltJuniorCollegeNum);
//                pageData.put("大专", juniorCollegeNum);
//                pageData.put("本科", undergraduateNum);
//                pageData.put("硕士", masterNum);
//                pageData.put("博士", doctorateNum);
//                pageData.put("博士后", postdoctorNum);
//                break;
//            default:
//                break;
//        }
//        result.add(pageData);
//        return R.data(result);
//    }
//
//    @Override
//    @YthTaskObserver
//    public boolean saveDjPartymemberBaseinfoByDataSource(String date) {
//
//        boolean flag = true;
//        Date start = DateUtil.parse(date, DateUtil.PATTERN_DATE);
//        //获取数据
//        //List<DjPartymemberBaseInfo> syncDatas = partyBaseInformationService.getSyncData(start, DateUtil.plusDays(start, 1));
//        List<DjPartymemberBaseInfo> syncDatas = partyBaseInformationService.getSyncData(null, null);
//
//
//        Map<String, String> orgSourceIds = orgService.getSourceId();
//        Map<String, String> userSourceIds = userService.getSourceId();
//
//        if (CollectionUtil.isNotEmpty(syncDatas)) {
//
//            //查询数据库存在的旧数据
//            List<DjPartymemberBaseInfo> allOldData = list(new LambdaQueryWrapper<DjPartymemberBaseInfo>()
//                    .eq(DjPartymemberBaseInfo::getDataSource, 3)
//                    .in(DjPartymemberBaseInfo::getDataSourceId, syncDatas.stream().map(DjPartymemberBaseInfo::getDataSourceId).collect(Collectors.toList())));
//
//            for (DjPartymemberBaseInfo syncData : syncDatas) {
//                //企业
//                String fillingUnitId = orgSourceIds.get(syncData.getFillingUnitId());
//                if (StringUtils.isNotBlank(fillingUnitId)) {
//                    syncData.setFillingUnitId(fillingUnitId);
//                    syncData.setCreateCompanyId(fillingUnitId);
//                }
//                String fiilingCreateUserCode = userSourceIds.get(syncData.getFillingCreateUserCode());
//                if (StringUtil.isNotBlank(fiilingCreateUserCode)){
//                    syncData.setFillingCreateUserCode(fiilingCreateUserCode);
//                }
//
//                if (CollectionUtil.isNotEmpty(allOldData)) {
//                    Optional<DjPartymemberBaseInfo> oldDjPartymemberBaseInfoOptional = allOldData.stream().filter(e -> syncData.getDataSourceId().equals(e.getDataSourceId())).findFirst();
//                    if (oldDjPartymemberBaseInfoOptional.isPresent()) {
//                        DjPartymemberBaseInfo djPartymemberBaseInfo = oldDjPartymemberBaseInfoOptional.get();
//                        djPartymemberBaseInfo.setFillingUnitId(fillingUnitId);
//                        djPartymemberBaseInfo.setFillingUnitName(syncData.getFillingUnitName());
//                        djPartymemberBaseInfo.setFillingCreateUserName(syncData.getFillingCreateUserName());
//                        djPartymemberBaseInfo.setFillingCreateUserCode(syncData.getFillingCreateUserCode());
//                        djPartymemberBaseInfo.setFillingTime(syncData.getFillingTime());
//                        djPartymemberBaseInfo.setPeopleNum(syncData.getPeopleNum());
//                        djPartymemberBaseInfo.setGtAge50Num(syncData.getGtAge50Num());
//                        djPartymemberBaseInfo.setAge3050Num(syncData.getAge3050Num());
//                        djPartymemberBaseInfo.setAge2535Num(syncData.getAge2535Num());
//                        djPartymemberBaseInfo.setLtAge25Num(syncData.getLtAge25Num());
//                        djPartymemberBaseInfo.setLtJuniorCollegeNum(syncData.getLtJuniorCollegeNum());
//                        djPartymemberBaseInfo.setJuniorCollegeNum(syncData.getJuniorCollegeNum());
//                        djPartymemberBaseInfo.setUndergraduateNum(syncData.getUndergraduateNum());
//                        djPartymemberBaseInfo.setMasterNum(syncData.getMasterNum());
//                        djPartymemberBaseInfo.setDoctorateNum(syncData.getDoctorateNum());
//                        djPartymemberBaseInfo.setPostdoctorNum(syncData.getPostdoctorNum());
//                        flag = updateById(djPartymemberBaseInfo);
//                    } else {
//                        flag = super.saveOrUpdate(syncData);
//                    }
//                } else {
//                    flag = super.saveOrUpdate(syncData);
//                }
//            }
//        }
//        return flag;
//    }
//}
